package com.baizhi.shane.shane.service.impl;

import com.baizhi.shane.common.base.Result;
import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.enums.ResultCode;
import com.baizhi.shane.shane.dto.*;
import com.baizhi.shane.shane.entity.*;
import com.baizhi.shane.shane.manager.CountVOManager;
import com.baizhi.shane.shane.service.*;
import com.baizhi.shane.shane.vo.*;
import com.baizhi.shane.sys.entity.SysUser;
import com.baizhi.shane.sys.manager.RedisManager;
import com.baizhi.shane.sys.service.SysUserService;
import com.baizhi.shane.sys.utils.CommonUtils;
import com.baizhi.shane.sys.vo.IdVO;
import com.baizhi.shane.sys.vo.SuccessedOutVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.shane.shane.mapper.ShaneQuestionMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author wbz12
* @description 针对表【shane_question(啥呢问题表)】的数据库操作Service实现
* @createDate 2023-07-25 22:33:37
*/
@Service
@Slf4j
public class ShaneQuestionServiceImpl extends ServiceImpl<ShaneQuestionMapper, ShaneQuestion>
    implements ShaneQuestionService{

    @Resource
    private SysUserService sysUserService;
    @Resource
    private CommonUtils commonUtils;

    @Resource
    private ResourceLoader resourceLoader;
    @Resource
    private ShaneQuestionMapper shaneQuestionMapper;
    @Resource
    private RedisManager redisManager;
    @Resource
    private UserLikeCountService userLikeCountService;

    @Resource
    private ShaneFollowService shaneFollowService;
    @Resource
    private CountVOManager countVOManager;

    /**
     * 新增一个问题
     *
     * @Author 闻柏智
     * @Date 16:56 2024/3/1
     * @Param questionInsertInVO
     * @return com.baizhi.shane.sys.vo.SuccessedOutVO
     **/
    @Override
    public SuccessedOutVO insertOne(QuestionInsertInVO questionInsertInVO) {
        if (questionInsertInVO == null) {
            Result.fail(ResultCode.PARAM_IS_INVALID);
        }
        ShaneQuestion shaneQuestion = new ShaneQuestion();
        BeanUtils.copyProperties(questionInsertInVO, shaneQuestion);

        shaneQuestion.setDisplayText(StringUtils.substring(shaneQuestion.getPlainText(), 0, 100));
        shaneQuestion.setUserId(redisManager.getUserId());
        if (shaneQuestion.getStatus() == null) {
            shaneQuestion.setStatus(SysMapping.STATUS_PUBLISHED);
        }

        boolean successed = this.save(shaneQuestion);
        SuccessedOutVO successedOutVO =
                commonUtils.boolToSuccessedOutVO(successed, SysMapping.INSERT_QUESTION_SUCCESS, SysMapping.INSERT_QUESTION_FAIL);

        return successedOutVO;
    }

    /**
     * 通过关键字模糊查询问题列表
     *
     * @Author 闻柏智
     * @Date 15:44 2024/1/22
     * @Param pageInVO
     * @return java.util.List<com.baizhi.shane.shane.vo.PageQuestionOutVO>
     **/
    @Override
    public List<PageQuestionOutVO> pageByContent(PageInVO pageInVO) {


        // 构造分页器
        PageInDTO pageInDTO = commonUtils.pageInVOToDTO(pageInVO, null);
        // 查询出问题列表
        List<PageQuestionMainOutDTO> pageQuestionMainOutDTOList = shaneQuestionMapper.pageByContent(pageInDTO);
        /* 无数据则返回空数组 */
        if (CollectionUtils.isEmpty(pageQuestionMainOutDTOList)) {
            return new ArrayList<>();
        }
        /* 提取问题ID列表 */
        List<Long> questionIdList = pageQuestionMainOutDTOList
                .stream()
                .map(PageQuestionMainOutDTO::getQuestionId)
                .collect(Collectors.toList());

        /* 查询出对应的关系信息 */
        // 点赞数量
        Map<Long, CountVO> likeMap = this.getQuestionLikeCountMapByQuestionIdList(questionIdList);
        // 收藏数量
        Map<Long, CountVO> collectMap = this.getQuestionCollectCountMapByQuestionIdList(questionIdList);
        // 评论数量
        Map<Long, CountVO> commentMap = this.getQuestionCommentCountMapByQuestionIdList(questionIdList);
        /* 初始化返回值 */
        ArrayList<PageQuestionOutVO> pageQuestionOutVOList = new ArrayList<>();
        /* 补充上各个关系表，构建VO */
        for (PageQuestionMainOutDTO pageQuestionMainOutDTO : pageQuestionMainOutDTOList) {
            PageQuestionOutVO pageQuestionOutVO = this.pageDTOToVO(pageQuestionMainOutDTO, likeMap, collectMap, commentMap);
            pageQuestionOutVOList.add(pageQuestionOutVO);
        }

        return pageQuestionOutVOList;
    }

    /**
     * 将单个分页列表出参DTO转为VO
     *
     * @Author 闻柏智
     * @Date 15:50 2024/1/22
     * @Param pageQuestionMainOutDTO
     * @Param likeMap:
     * @Param collectMap:
     * @Param commentMap:
     * @return com.baizhi.shane.shane.vo.PageQuestionOutVO
     **/
    private PageQuestionOutVO pageDTOToVO(PageQuestionMainOutDTO pageQuestionMainOutDTO,
                                          Map<Long, CountVO> likeMap,
                                          Map<Long, CountVO> collectMap,
                                          Map<Long, CountVO> commentMap
    ) {
        PageQuestionOutVO pageQuestionOutVO = new PageQuestionOutVO();
        BeanUtils.copyProperties(pageQuestionMainOutDTO,pageQuestionOutVO);
        /*补充关系*/
        Long questionId = pageQuestionOutVO.getQuestionId();
        pageQuestionOutVO.setLikeCount(likeMap.get(questionId).getTheCount().longValue());
        pageQuestionOutVO.setCollectCount(collectMap.get(questionId).getTheCount().longValue());
        pageQuestionOutVO.setCommentCount(commentMap.get(questionId).getTheCount().longValue());
        pageQuestionOutVO.setNowUserLiked(likeMap.get(questionId).getNowUserRelIt());
        pageQuestionOutVO.setNowUserCollected(collectMap.get(questionId).getNowUserRelIt());
        pageQuestionOutVO.setNowUserCommented(commentMap.get(questionId).getNowUserRelIt());
        pageQuestionOutVO.setNowUserLikedRelId(likeMap.get(questionId).getNowUserRelId());
        pageQuestionOutVO.setNowUserCollectedRelId(collectMap.get(questionId).getNowUserRelId());

        // 默认不显示评论树
        pageQuestionOutVO.setShowComment(SysMapping.NOT_DISPLAY_COMMENT_TREE);
        return pageQuestionOutVO;
    }

    /**
     * 测试初始化的时候用的
     *
     * @Author 闻柏智
     * @Date 16:28 2024/3/2
     * @Param
     * @return void
     **/
    @Override
//    @Transactional(rollbackFor = RuntimeException.class)
    public void testInitAssociationTable() {
        /* 查所有问题ID */
//        LambdaQueryWrapper<ShaneQuestion> qwSq = new LambdaQueryWrapper<>();
//        qwSq.select(ShaneQuestion::getId);
//        List<ShaneQuestion> questionList = this.list(qwSq);
//        List<Long> questionIdList = questionList
//                .stream()
//                .map(ShaneQuestion::getId)
//                .collect(Collectors.toList());

        /* 查所有回答ID */
//        LambdaQueryWrapper<ShaneAnswer> qwSa = new LambdaQueryWrapper<>();
//        qwSa.select(ShaneAnswer::getId);
//        List<ShaneAnswer> answerList = shaneAnswerService.list(qwSa);
//        List<Long> answerIdList = answerList
//                .stream()
//                .map(ShaneAnswer::getId)
//                .collect(Collectors.toList());

//        /* 查所有文章ID */
//        LambdaQueryWrapper<ShaneEssay> qwSe = new LambdaQueryWrapper<>();
//        qwSe.select(ShaneEssay::getId);
//        List<ShaneEssay> shaneEssayList = shaneEssayService.list(qwSe);
//        List<Long> essayIdList = shaneEssayList
//                .stream()
//                .map(ShaneEssay::getId)
//                .collect(Collectors.toList());

        /* 查所有用户ID */
        LambdaQueryWrapper<SysUser> qwSu = new LambdaQueryWrapper<>();
        qwSu.select(SysUser::getId);
        List<SysUser> userList = sysUserService.list(qwSu);
        List<Long> userIdList = userList
                .stream()
                .map(SysUser::getId)
                .collect(Collectors.toList());
        /* 查所有用户的各种个数 */
        LambdaQueryWrapper<UserLikeCount> qwUlc = new LambdaQueryWrapper<>();
//        qwUlc.select(UserLikeCount::getId,UserLikeCount::getQuestionCollectCount);
        List<UserLikeCount> userLikeCountList = userLikeCountService.list(qwUlc);

//        /* 用户收藏问题表 */
//        List<ShaneCollectQuestion> collectQuestionList = new ArrayList<>();
//        for (UserLikeCount userLikeCount : userLikeCountList) {
//            List<Long> questionIdRamdomList = this.getRandomFromList(questionIdList, userLikeCount.getQuestionCollectCount());
//            for (Long questionId : questionIdRamdomList) {
//                ShaneCollectQuestion shaneCollectQuestion = new ShaneCollectQuestion();
//                shaneCollectQuestion.setUserId(userLikeCount.getId());
//                shaneCollectQuestion.setQuestionId(questionId);
//                collectQuestionList.add(shaneCollectQuestion);
//            }
//
//        }
//        shaneCollectQuestionService.saveBatch(collectQuestionList);

        /* 用户收藏回答表 */
//        List<ShaneCollectAnswer> collectAnswerList = new ArrayList<>();
//        for (UserLikeCount userLikeCount : userLikeCountList) {
//            List<Long> answerIdRamdomList = this.getRandomFromList(answerIdList, userLikeCount.getAnswerCollectCount());
//            for (Long answerId : answerIdRamdomList) {
//                ShaneCollectAnswer shaneCollectAnswer = new ShaneCollectAnswer();
//                shaneCollectAnswer.setUserId(userLikeCount.getId());
//                shaneCollectAnswer.setAnswerId(answerId);
//                collectAnswerList.add(shaneCollectAnswer);
//            }
//
//        }
//        shaneCollectAnswerService.saveBatch(collectAnswerList, 5000);

        /* 用户收藏文章表 */
//        List<ShaneCollectEssay> collectEssayList = new ArrayList<>();
//        for (UserLikeCount userLikeCount : userLikeCountList) {
//            List<Long> essayIdRandomList = this.getRandomFromList(essayIdList, userLikeCount.getAnswerCollectCount());
//            for (Long essayId : essayIdRandomList) {
//                ShaneCollectEssay shaneCollectEssay = new ShaneCollectEssay();
//                shaneCollectEssay.setUserId(userLikeCount.getId());
//                shaneCollectEssay.setEssayId(essayId);
//                collectEssayList.add(shaneCollectEssay);
//            }
//        }
//        shaneCollectEssayService.saveBatch(collectEssayList, 10000);

        /* 用户关注用户表 */
        List<ShaneFollow> shaneFollowList = new ArrayList<>();
        Random random = new Random();
        for (UserLikeCount userLikeCount : userLikeCountList) {

            int randomNumber = random.nextInt(10); // 生成0到9之间的随机数
            List<Long> userIdRandomList = this.getRandomFromList(userIdList, randomNumber);
            for (Long userId : userIdRandomList) {
                ShaneFollow shaneFollow = new ShaneFollow();
                shaneFollow.setFollowerId(userLikeCount.getId());
                shaneFollow.setBefollowedId(userId);
                shaneFollowList.add(shaneFollow);
            }
        }
        shaneFollowService.saveBatch(shaneFollowList);



        log.info("O了");

        return;


    }

    /**
     * 从List随机取出n个数
     *
     * @Author 闻柏智
     * @Date 19:45 2023/9/29
     * @Param list
     * @Param n:
     * @return java.util.List<java.lang.Long>
     **/
    @Override
    public List<Long> getRandomFromList(List<Long> list, int n) {
        // 复制List
        List<Long> newList = new ArrayList<>(list);
        // 首先将List打乱顺序
        Collections.shuffle(list);
        // 取出前 n 个数
        List<Long> result = newList.subList(0, n);

        return result;
    }

    /**
     * 删除一个问题-通过ID
     *
     * @Author 闻柏智
     * @Date 18:05 2024/1/1
     * @Param idVO
     * @return com.baizhi.shane.sys.vo.SuccessedOutVO
     **/
    @Override
    public SuccessedOutVO deleteOneQuestionById(IdVO idVO) {
        Long id = idVO.getId();
        boolean success = this.removeById(id);
        SuccessedOutVO successedOutVO = commonUtils.boolToSuccessedOutVO(success, null, null);
        return successedOutVO;
    }

    /**
     * 通过关键字查询问题列表的元素数量和分页个数
     *
     * @Author 闻柏智
     * @Date 20:11 2024/1/1
     * @Param pageInVO
     * @return com.baizhi.shane.shane.vo.CountAndPageOutVO
     **/
    @Override
    public CountAndPageOutVO countAndPageByContent(PageInVO pageInVO) {
        String content = pageInVO.getContent();
        Integer pageSize = pageInVO.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }
        double pageSizeDouble = pageSize.doubleValue();
        Integer count = this.shaneQuestionMapper.countAndPageByContent(content);

        int pageCountInt = (int) Math.ceil(count / pageSizeDouble);
        Integer pageCount = pageCountInt;

        CountAndPageOutVO countAndPageOutVO = new CountAndPageOutVO();
        countAndPageOutVO.setMaxCount(count);
        countAndPageOutVO.setPageCount(pageCount);

        return countAndPageOutVO;
    }

    /**
     * 通过问题ID列表查询这些问题的标签列表
     *
     * @Author 闻柏智
     * @Date 17:22 2024/1/7
     * @Param idListStr
     * @return java.util.List<com.baizhi.shane.shane.vo.QuestionLabelOutVO>
     **/
    @Override
    public List<QuestionLabelOutVO> getQuestionLabelListByQuestionIdList(String idListStr) {
        // 入参字符串转为Long列表
        List<Long> questionIdList = commonUtils.idListStrToLongList(idListStr);
        // 从数据库查数据
        List<QuestionLabelOutDTO> questionLabelOutDTOList =
                this.shaneQuestionMapper.getQuestionLabelListByQuestionIdList(questionIdList);
        /* 按照问题ID分组 */
        Map<Long, List<QuestionLabelOutDTO>> idEntityMap = questionLabelOutDTOList
                .stream()
                .collect(Collectors.groupingBy(QuestionLabelOutDTO::getQuestionId));
        // 提取问题ID集合
        Set<Long> questionIdSet = idEntityMap.keySet();
        // 创建VO列表
        List<QuestionLabelOutVO> questionLabelOutVOList = new ArrayList<>();
        /* 构建返回值列表 */
        for (Long questionId : questionIdSet) {
            QuestionLabelOutVO questionLabelOutVO = new QuestionLabelOutVO();
            questionLabelOutVO.setQuestionLabelOutDTOList(new ArrayList<>());
            List<QuestionLabelOutDTO> labelList = idEntityMap.get(questionId);
            questionLabelOutVO.setQuestionId(questionId);
            if (!CollectionUtils.isEmpty(labelList)) {
                questionLabelOutVO.setQuestionLabelOutDTOList(labelList);
            }
            questionLabelOutVOList.add(questionLabelOutVO);

        }
        return questionLabelOutVOList;

    }

    /**
     * 通过问题ID查询单个问题的标签
     *
     * @Author 闻柏智
     * @Date 17:28 2024/1/7
     * @Param questionId
     * @return com.baizhi.shane.shane.vo.QuestionLabelOutVO
     **/
    @Override
    public QuestionLabelOutVO getQuestionLabelByQuestionId(Long questionId) {
        String idListStr = questionId.toString() + SysMapping.COMMA;
        List<QuestionLabelOutVO> questionLabelOutVOList = this.getQuestionLabelListByQuestionIdList(idListStr);
        if (CollectionUtils.isEmpty(questionLabelOutVOList)) {
            return new QuestionLabelOutVO();
        }
        QuestionLabelOutVO questionLabelOutVO = questionLabelOutVOList.get(0);
        return questionLabelOutVO;
    }

    /**
     * 通过问题ID列表查询这些问题的点赞数量
     *
     * @Author 闻柏智
     * @Date 22:53 2024/1/17
     * @Param questionIdList
     * @return java.util.List<com.baizhi.shane.shane.vo.CountVO>
     **/
    @Override
    public List<CountVO> getQuestionLikeCountByQuestionIdList(List<Long> questionIdList) {
        Long userId = this.redisManager.getUserId();
        List<CountVO> countVOList = this.shaneQuestionMapper.getQuestionLikeCountByQuestionIdList(questionIdList, userId);
        return countVOList;
    }

    /**
     * 通过问题ID列表查询这些问题的点赞数量,并且转成Map
     *
     * @Author 闻柏智
     * @Date 21:51 2024/1/20
     * @Param questionIdList
     * @return java.util.Map<java.lang.Long,java.util.List<com.baizhi.shane.shane.vo.CountVO>>
     **/
    @Override
    public Map<Long, CountVO> getQuestionLikeCountMapByQuestionIdList(List<Long> questionIdList) {
        List<CountVO> questionLikeCountByQuestionIdList = this.getQuestionLikeCountByQuestionIdList(questionIdList);
        Map<Long, CountVO> likeMap = questionLikeCountByQuestionIdList
                .stream()
                .collect(Collectors.toMap(
                        CountVO::getEntityId, // Key属性
                        Function.identity()  // Value属性
                ));

        return likeMap;
    }

    /**
     * 通过问题ID查询单个问题的点赞数量
     *
     * @Author 闻柏智
     * @Date 23:11 2024/1/17
     * @Param questionId
     * @return com.baizhi.shane.shane.vo.CountVO
     **/
    @Override
    public CountVO getOneQuestionLikeCountByQuestionId(Long questionId) {
        List<Long> questionIdList = new ArrayList<Long>() {{
            add(questionId);
        }};
        List<CountVO> countVOList = this.getQuestionLikeCountByQuestionIdList(questionIdList);
        CountVO countVO = countVOManager.countVOListToOneVO(countVOList, questionId);
        return countVO;
    }

    /**
     * 通过问题ID列表查询这些问题的收藏数量
     *
     * @Author 闻柏智
     * @Date 13:21 2024/1/18
     * @Param questionIdList
     * @return java.util.List<com.baizhi.shane.shane.vo.CountVO>
     **/
    @Override
    public List<CountVO> getQuestionCollectCountByQuestionIdList(List<Long> questionIdList) {
        Long userId = this.redisManager.getUserId();
        List<CountVO> countVOList = this.shaneQuestionMapper.getQuestionCollectCountByQuestionIdList(questionIdList, userId);
        return countVOList;
    }

    /**
     * 通过问题ID列表查询这些问题的收藏数量,并且转成Map
     *
     * @Author 闻柏智
     * @Date 22:02 2024/1/20
     * @Param questionIdList
     * @return java.util.Map<java.lang.Long,com.baizhi.shane.shane.vo.CountVO>
     **/
    @Override
    public Map<Long, CountVO> getQuestionCollectCountMapByQuestionIdList(List<Long> questionIdList) {
        List<CountVO> questionCollectCountByQuestionIdList = this.getQuestionCollectCountByQuestionIdList(questionIdList);
        Map<Long, CountVO> collectMap = questionCollectCountByQuestionIdList
                .stream()
                .collect(Collectors.toMap(
                        CountVO::getEntityId,
                        Function.identity()
                ));
        return collectMap;
    }

    /**
     * 通过问题ID查询单个问题的收藏数量
     *
     * @Author 闻柏智
     * @Date 13:25 2024/1/18
     * @Param questionId
     * @return com.baizhi.shane.shane.vo.CountVO
     **/
    @Override
    public CountVO getOneQuestionCollectCountByQuestionId(Long questionId) {
        List<Long> questionIdList = new ArrayList<Long>() {{
            add(questionId);
        }};
        List<CountVO> countVOList = this.getQuestionCollectCountByQuestionIdList(questionIdList);
        CountVO countVO = countVOManager.countVOListToOneVO(countVOList, questionId);
        return countVO;
    }

    /**
     * 通过问题ID列表查询这些问题的评论数量
     *
     * @Author 闻柏智
     * @Date 13:30 2024/1/18
     * @Param questionIdList
     * @return java.util.List<com.baizhi.shane.shane.vo.CountVO>
     **/
    @Override
    public List<CountVO> getQuestionCommentCountByQuestionIdList(List<Long> questionIdList) {
        Long userId = this.redisManager.getUserId();
        List<CountVO> countVOList = this.shaneQuestionMapper.getQuestionCommentCountByQuestionIdList(questionIdList, userId);
        return countVOList;
    }

    /**
     * 通过问题ID列表查询这些问题的评论数量，并且转为Map
     *
     * @Author 闻柏智
     * @Date 22:06 2024/1/20
     * @Param questionIdList
     * @return java.util.Map<java.lang.Long,com.baizhi.shane.shane.vo.CountVO>
     **/
    @Override
    public Map<Long, CountVO> getQuestionCommentCountMapByQuestionIdList(List<Long> questionIdList) {
        List<CountVO> questionCommentCountByQuestionIdList = this.getQuestionCommentCountByQuestionIdList(questionIdList);
        Map<Long, CountVO> commentMap = questionCommentCountByQuestionIdList
                .stream()
                .collect(Collectors.toMap(
                        CountVO::getEntityId,
                        Function.identity()
                ));
        return commentMap;
    }

    /**
     * 通过问题ID查询单个问题的评论数量
     *
     * @Author 闻柏智
     * @Date 13:39 2024/1/18
     * @Param questionId
     * @return com.baizhi.shane.shane.vo.CountVO
     **/
    @Override
    public CountVO getOneQuestionCommentCountByQuestionId(Long questionId) {
        List<Long> questionIdList = new ArrayList<Long>() {{
            add(questionId);
        }};
        List<CountVO> countVOList = this.getQuestionCommentCountByQuestionIdList(questionIdList);
        CountVO countVO = countVOManager.countVOListToOneVO(countVOList, questionId);
        return countVO;
    }

    /**
     * 通过ID查询单个问题信息
     *
     * @Author 闻柏智
     * @Date 16:07 2024/2/29
     * @Param questionId
     * @return com.baizhi.shane.shane.vo.GetOneQuestionOutVO
     **/
    @Override
    public GetOneQuestionOutVO getOneQuestionById(Long questionId) {
        // 查询
        GetOneQuestionOutDTO getOneQuestionOutDTO = this.shaneQuestionMapper.getOneQuestionById(questionId);
        // 初始化返回值
        GetOneQuestionOutVO getOneQuestionOutVO = new GetOneQuestionOutVO();
        // 复制属性
        BeanUtils.copyProperties(getOneQuestionOutDTO, getOneQuestionOutVO);

        /* 获取关系 */
        CountVO likeVO = this.getOneQuestionLikeCountByQuestionId(questionId);
        CountVO collectVO = this.getOneQuestionCollectCountByQuestionId(questionId);
        CountVO commentVO = this.getOneQuestionCommentCountByQuestionId(questionId);

        /* 赋值关系值 */
        getOneQuestionOutVO.setLikeCount(likeVO.getTheCount().longValue());
        getOneQuestionOutVO.setCollectCount(collectVO.getTheCount().longValue());
        getOneQuestionOutVO.setCommentCount(commentVO.getTheCount().longValue());
        getOneQuestionOutVO.setNowUserLiked(likeVO.getNowUserRelIt());
        getOneQuestionOutVO.setNowUserCollected(collectVO.getNowUserRelIt());
        getOneQuestionOutVO.setNowUserCommented(commentVO.getNowUserRelIt());
        getOneQuestionOutVO.setNowUserLikedRelId(likeVO.getNowUserRelId());
        getOneQuestionOutVO.setNowUserCollectedRelId(collectVO.getNowUserRelId());

        // 评论树默认不显示
        getOneQuestionOutVO.setShowComment(0l);

        return getOneQuestionOutVO;
    }

    /**
     * 我发布过的问题
     *
     * @Author 闻柏智
     * @Date 21:15 2024/3/3
     * @Param
     * @return java.util.List<com.baizhi.shane.shane.entity.ShaneQuestion>
     **/
    @Override
    public List<ShaneQuestion> myPosted() {
        // 获取当前登陆人ID
        Long userId = redisManager.getUserId();
        /* 获取所有的问题 */
        List<ShaneQuestion> shaneQuestionList = this.lambdaQuery()
                .eq(ShaneQuestion::getDelFlag, 0)
                .eq(ShaneQuestion::getUserId, userId)
                .list();
        return shaneQuestionList;

    }

    /**
     * 通过ID编辑问题
     *
     * @Author 闻柏智
     * @Date 22:34 2024/3/3
     * @Param shaneQuestion
     * @return com.baizhi.shane.sys.vo.SuccessedOutVO
     **/
    @Override
    public SuccessedOutVO updateOneById(ShaneQuestion shaneQuestion) {
        shaneQuestion.setDisplayText(StringUtils.substring(shaneQuestion.getPlainText(), 0, 100));
        boolean successed = this.updateById(shaneQuestion);
        SuccessedOutVO successedOutVO =
                commonUtils.boolToSuccessedOutVO(successed, SysMapping.CHANGE_SUCCESS, SysMapping.CHANGE_FAIL);
        return successedOutVO;
    }

    /**
     * 我收藏的问题
     *
     * @Author 闻柏智
     * @Date 14:28 2024/3/4
     * @Param
     * @return java.util.List<com.baizhi.shane.shane.entity.ShaneQuestion>
     **/
    @Override
    public List<ShaneQuestion> myCollected() {
        // 获取当前登陆人ID
        Long userId = redisManager.getUserId();
        List<ShaneQuestion> shaneQuestionList = shaneQuestionMapper.myCollected(userId);
        return shaneQuestionList;
    }

    /**
     * 用户管理信息列表查询
     *
     * @Author 闻柏智
     * @Date 17:38 2024/3/4
     * @Param pageInVO
     * @return java.util.List<com.baizhi.shane.shane.dto.UserManagerOutDTO>
     **/
    @Override
    public List<UserManagerOutDTO> userManagerList(PageInVO pageInVO) {
        // 构造分页器
        PageInDTO pageInDTO = commonUtils.pageInVOToDTO(pageInVO, null);
        List<UserManagerOutDTO> userManagerOutDTOList = this.shaneQuestionMapper.userManagerList(pageInDTO);
        return userManagerOutDTOList;
    }

    /**
     * 用户管理信息列表数量查询
     *
     * @Author 闻柏智
     * @Date 17:38 2024/3/4
     * @Param pageInVO
     * @return com.baizhi.shane.shane.vo.CountAndPageOutVO
     **/
    @Override
    public CountAndPageOutVO userManagerCountAndPageByContent(PageInVO pageInVO) {
        String content = pageInVO.getContent();
        Integer pageSize = pageInVO.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }
        double pageSizeDouble = pageSize.doubleValue();
        Integer count = this.shaneQuestionMapper.userManagerCountAndPageByContent(content);

        int pageCountInt = (int) Math.ceil(count / pageSizeDouble);
        Integer pageCount = pageCountInt;

        CountAndPageOutVO countAndPageOutVO = new CountAndPageOutVO();
        countAndPageOutVO.setMaxCount(count);
        countAndPageOutVO.setPageCount(pageCount);

        return countAndPageOutVO;
    }

}




